package com.shopping.Service.ServiceImpl;

import com.shopping.Entity.OrderInfo;
import com.shopping.Entity.UserInfo;
import com.shopping.Entity.UserScore;
import com.shopping.Service.TradeService;
import com.shopping.Service.UserInfoService;
import com.shopping.Service.UserScoreService;
import com.shopping.repository.OrderInfoRepository;
import com.shopping.utils.DateTimeUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class TradeServiceImpl implements TradeService {
    @Autowired
    UserInfoService userInfoService;
    @Autowired
    DateTimeUtil dateTimeUtil;
    @Autowired
    UserScoreService userScoreService;
    @Autowired
    OrderInfoRepository orderInfoRepository;

    @Override
    public boolean goodsBillCount(List<OrderInfo> orderInfoList) {
        ArrayList<OrderInfo> orderInfos = new ArrayList<>();
        orderInfos.addAll(orderInfoList);
        int finalScore = finalScore(orderInfos);
        int userId = orderInfos.get(0).getId();
        //存库
        UserScore userScore = new UserScore(0,userId,1,0,finalScore, (Timestamp) new Date());
        UserInfo userInfo = userInfoService.getUserByid(userId);
        float cost = 0.0f;
        for (OrderInfo o:orderInfos) {
            cost+=o.getAmount();
            //没有list的联动标记,因此插入order之后,无法追溯同一次下单中的不同商品的关联性
            orderInfoRepository.save(o);
        }
        UserScore u  = userScoreService.save(userScore);
        if (u != null){
            userInfo.setAccount(userInfo.getAccount()-cost);
            userInfo.setSumscore(userInfo.getSumscore()+finalScore);
            userInfoService.updateUserInfo(userInfo);
            return true;
        }
        return false;
    }

    public int finalScore(List<OrderInfo> orderInfoList){
        ArrayList<OrderInfo> orderInfos = new ArrayList<>();
        orderInfos.addAll(orderInfoList);
        //获取总金额,商品数量
        int totalNum = 0;
        float totalCost =0.0f;
        int userId = orderInfos.get(0).getUserid();
        Date orderDate = orderInfos.get(0).getCreateTime();
        for (OrderInfo info: orderInfos) {
            totalCost += info.getAmount();
            totalNum += info.getQuantity();
        }
        //算基础分
        float baseScore = (float) Math.round(totalCost/10);
        //时间规则
        float timeRules = timeRules(baseScore,userId,orderDate);
        //数量&金额
        float numRules = numRules(baseScore,totalNum,totalCost);

        return Math.round(Math.max(timeRules,numRules));
    }


    private float numRules(float baseScore, int totalNum, float totalCost) {
        if (totalNum>=100 ||totalCost>=1000){
            return baseScore*1.3f;
        }else if (totalNum>=50 || totalCost>=500){
            return baseScore*1.2f;
        }else if(totalNum>=10||totalCost>=200){
            return baseScore*1.1f;
        }
        return baseScore;
    }

    private float timeRules(float baseScore,int userId,Date date) {
        //获取 当前时间
        String today = dateTimeUtil.getMonthAndDay(date);
        //判断生日
        //获取生日信息
        String birthday = dateTimeUtil.getMonthAndDay(userInfoService.getUserByid(userId).getBirthday());
        if (today.equals(birthday)){
            return baseScore*1.3f;
        }
        //判断双十一
        if (today.equals("11-11")){
            return baseScore*1.2f;
        }
        //判断618
        if (today.equals("06-18")){
            return baseScore*1.15f;
        }
        //判断时间段
        int now = dateTimeUtil.getTimeNum(date);
        if (now>=80000&&now<=120000){
            return baseScore*1.05f;
        }
        return baseScore;
    }

}
